home *** CD-ROM | disk | FTP | other *** search
/ Beginning Mac Programming / Beginning Mac Programming.bin / Open Me for REALbasic 3 / REALbasic 3.2 / Example Projects / Techniques / ZegsRule / Customization next >
Text File  |  2000-04-26  |  10KB  |  104 lines

  1.  
  2. Customization
  3.  
  4.             If the built in rulers are not what you want then you can design your own ruler. This is not difficult but can take some time if you need to draw at many different scales (zooming). It is important to realize that ZegsRuler (with its helper classes textData and lineData) contains all the functionality required to draw a ruler and that GraphicsRuler etc are just covers for this functionality. If one of the rulers is not quite right at a particular scale it can be modified or completely replaced.  In order to customize a ruler you have to understand major lines,minor lines and unitLength. These are much harder to explain than to use!  Looking at one of the ruler construction methods (eg inch10) of the GraphicRuler class as you read this may help.
  5.  
  6.         Major lines. To create a ruler you have to specify at least 1 major line in each scale range that you support. Major lines extend completely across the ruler and usually have text associated with them. They can also have their own color and thickness. It is possible to have more than one major line ( say a 10 cm division on a cm ruler) if you need the line to look different (color,width text etc) from other major lines.
  7.         Major lines are drawn at a multiple of the unitLength (UL) of the ruler multiplied by the current scale. Therefore a ruler with a unitLength of 72 and current scale of 1 with a multiplier of 1 will draw a line every 72 pixels. If the scale is reduced to .5 the lines will be drawn every 36 pixels, which may be too close for you. Therefore at some point between a scale of 1 and .5 you may decide to recreate a ruler with different markings. This is how you build up a ruler. 
  8.  
  9.         UnitLength(UL) - closely associated with major lines is the idea of unitLength. The UL of a ruler is used to calculate how far apart the major lines are drawn, however the relationship is not direct because this distance also depends on the scale and the multiplier of the major line. In general its best to set the UL to the number of pixels between the smallest major line at 100% scale and not to change it.  This is fairly arbitrary but for inches it could be 72 because 1 inch is 72 pixels at the standard resolution of your monitor. Using the same logic a cm ruler could be initialised to 72/2.54 because there are 2.54 cm in an inch. A ruler measuring pixels may have it set to 100. A ruler measuring seconds,frames of a movie, etc should be based on how far apart you want your major lines at full scale.
  10.  
  11.         To create a major line you use the SetMajor method:
  12.  
  13. SetMajor(multiplier as double, penWidth as integer,skip as integer,drawZero as Boolean,[lineColor as Color])
  14.  
  15. multiplier - a major line is drawn every UL * scale * multiplier along the ruler.
  16. penWidth - how wide you would like the line. The length of all major lines are the same.
  17. skip - prevents double drawing of lines (and text) when you have more than one major line. If you only have 1 major line then set it to 0, otherwise set it to how often you would like a space
  18. drawZero - if you have more than one major line then set to true the one you wish to draw the zero line.
  19. lineColor - optional. If not used it will use the foreColor at the time this method is called.
  20.  
  21. eg: At first the meaning of multiplier and skip are probably not clear. Say you want a ruler that has a thin blue major line every 100 pixels at full scale but you want a different line (thicker red) at every 500 pixels. The following will do that ( put it in the Custom2 method of the project):
  22.                     
  23. InitRuler 100
  24.                 
  25. SetMajor(1,1,5,false,RGB(0,0,255))
  26. SetMajor(5,2,0,true,RGB(255,0,0))
  27.  
  28.         The first SetMajor draws a line 1 * UL * scale pixels (ie. 100) and skips every fifth line. It also doesnt draw the zero. The second SetMajor draws a line every 5 * UL * scale pixels (ie. 500),doesnt skip any lines and draws the zero. If you comment out the second SetMajor you will see the space left at 0,500,1500 etc for the red line (use the slider to adjust the scale,or move the origin). 
  29.         Now say you want a green line every 1000 pixels. Change the second SetMajor to (5,2,2,false,RGB(255,0,0)). This will leave a space every 2 * 5 * UL pixels. Then add SetMajor(10,3,0,true,RGB(0,255,0)).
  30.  
  31.     If you use the scale slider you will see that the blue lines get too close together at small scales and too far apart at larger scales, so you have to create a new ruler. You create a new ruler every time you want to change the "look" of a ruler at a particular scale or scale range.
  32.  
  33. eg (put this in custom2)
  34.  
  35. InitRuler 100
  36.  
  37. if scale < 0.5 then
  38. SetMajor(2,1,5,false,RGB(0,0,255))
  39. SetMajor(10,3,0,true,RGB(0,255,0))
  40. elseif scale < 2 then
  41. SetMajor(1,1,5,false,RGB(0,0,255))
  42. SetMajor(5,2,2,false,RGB(255,0,0))
  43. SetMajor(10,3,0,true,RGB(0,255,0))
  44. else
  45. SetMajor(.5,1,2,false,RGB(255,0,255))
  46. SetMajor(1,1,5,false,RGB(0,0,255))
  47. SetMajor(5,2,2,false,RGB(255,0,0))
  48. SetMajor(10,3,0,true,RGB(0,255,0))
  49. end
  50.  
  51.         In the above ruler, at scales of less than .5 we draw every 2 * UL and skip every 5 lines. We then draw our 1000 line (10 * UL) again, without skipping. The first line will miss the 1000 mark because 2 * 5 = 10. Above a scale of 2 we introduce a 50 pixel purple mark ( .5 * UL). It skips every second time because we also still have the 100 mark (1 * UL). If you use fixed scales you could use a case statement instead of if..then.
  52.  
  53. InitRuler(unitLength as double)
  54.         Must be called before a ruler is built with SetMajor etc. The unitLength (UL) can be any value but 0. See above for details
  55.  
  56. SetMinor(howMany as integer, lineLength as integer,skip as integer,[lineColor as Color]) - works very much the same as SetMajor and is used to layout the minor marks. Minor marks are shorter lines drawn between the major marks. They also never have text. 
  57.  
  58. howmany - always greater than 1. The ruler will draw  howMany marks between each of the smallest major lines.
  59. lineLength - length of line in pixels. All minor lines are the same width
  60. skip - same purpose as with SetMajor. Used to jump over other minor and major marks
  61. lineColor - optional. Will use foreColor at time of calling this method if not used.
  62.  
  63. eg: adding SetMinor(2,8,2)  to the above in each scale range will draw a 8 pixel line halfway between each of the smallest major lines.
  64. Adding SetMinor(4,5,2) will add 1/4 marks, 5 pixels long,skipping the 1/2 marks (and major lines), or SetMinor(10,5,5) will add 1/10 marks, jumping over the 1/2 marks
  65.  
  66. SetText(textmultiplier as double,repeat as Boolean,suffix as string,[textColor as Color]) - used to set the text for the major lines. If you want a major line to have text you must call this before you add another major line ie. SetMajor(), SetText(), SetMajor(), SetText() etc.
  67.  
  68. textmultiplier - numbering starts from 0. The text multiplier determines how quickly the number increases. A multiplier of 1 will cause each major line to be numbered 0,1,2,3,4,5.... A multiplier of 2 will cause 0,2,4,6,8... A multiplier of 0.5 will cause .5,1.0,1.5 etc. A negative multiplier will cause the numbering to be reversed which can be useful, especially for vertical scales.
  69. repeat - because text is associated with a major line it will skip with the line. If repeat is set to true then then the numbering will begin repeating after the skip. This way you can have an inch ruler that starts again at 1 after every foot or continue on at 13 etc.
  70. suffix - text added after the number. Can be anything eg in,cm,ft,yd,ly (light year),km,mm,sec,min, x10^2.
  71. color - optional. Will use foreColor at time of calling this method if not used.
  72.  
  73. The line that has drawZero set to true will also draw the number 0 if it has text associated with it.
  74.  
  75.  
  76. Important: It is important to get the skip value correct. Most of the time your ruler will look OK if you draw the same mark twice *but* because of floating point rounding errors it will occasionally draw a line 1 pixel out and they will appear next to each other!
  77.  
  78.  
  79.  
  80. Other useful Methods:
  81.  
  82. AdjustText(x as integer,y as integer)
  83.     This allows you to shift the position of the text slightly if its not quite right. It works for the whole ruler, not individual major lines.
  84.  
  85. UserDrawsText(g as Graphics,s as string,count as integer,location as integer,space as integer)
  86. g - the graphics object to draw into
  87. s - the text that would normally be drawn
  88. count - which major line is being drawn
  89. location - where the line will be drawn, from the left if its a horizontal ruler, from the top if vertical 
  90. space - distance between lines, so you can center text etc
  91.  
  92.     This allows you to draw your own text instead of one of the built in methods. It gets called when the ruler is being drawn if the textLocation is  > 19. You can have many different textLocations (> 19) and this method will be called for all of them. You can then use a select case to distinguish between them. The graphics font,size,color etc is already set up but you can change them.The Custom1 type (ruler project) and the enclosed GridClass class (other examples project) shows how you can use this method.
  93.  
  94.  
  95. ChangeFont(font as string,size as integer,bold as Boolean,italic as Boolean)
  96. font - the name of the font
  97. size - text size
  98. bold - true or false
  99. italic - true or false
  100.  
  101. This is a convenience routine to change text data. Can be also called from within a ruler building routine to easily change the font. eg if you have a cm ruler you could make every 10 cm bold by calling this just before setting the text for that mark. However you would have to have two major marks, one for the cm and one for the 10 cm to do this.
  102.  
  103.  
  104.